from pygame.sprite import Sprite
from controls import Controls
from menu import *
import gameobjects
import math

class Player(Sprite):
    def __init__(self,game_engine,sprite,keys=None,name="John Doe",home_base=3,id=None):
        Sprite.__init__(self) #call Sprite initializer        
        self.game_engine = game_engine
        self.sprite = sprite
        self.sprite.owner = self
        self.ship = sprite
        self.name = name
        self.score = 0
        self.home_base = home_base
        self.startingPos = (0,0)
        self.vel_change = 5600*game_engine.relative_speed # Ships engine thrust
        self.ang_change = math.pi*1.8
        self.selecting = 0    # flag set if player is in select menu
        self.respawn_time = Tick(1)
        self.changed = 0 # flag to tell if player has got controller input
        self.lives = 5
        # Players ingame menu
        self.ws = WeaponSelector(self)
        # Let the menu init me with menu default weapons
        self.ws.setup()

        if keys:
            self.controls = Controls(keys,self,self.ws)
        else:
            self.controls = None

        # give my self a uniqe gameobject ID, same as my ship sprite
        if id ==None:
            self.id = gameobjects.GameObject.ID #sprite.id    
            gameobjects.GameObject.ID +=1
        else:
            self.id = id
        print "player with id:%d created"%self.id

    def setGameEngine(self,ge):
        self.game_engine = ge

    def alive(self):
        return self.lives
    def kill(self):
        self.ship.kill()
        Sprite.kill(self)

    def updateKeyPress(self,key_state,dt):
        ''' update the player acording to key_state '''
        self.controls.update(key_state,dt)

    def update(self,dt):
        self.ws.update(dt)
        
    def reset(self):
        ' Resets player ship values '
        self.ship.reset()
        self.ship.pos(self.startingPos[0],self.startingPos[1])

    def isDocked(self,dt=0):
        ' Checks if ship is on a base tile and invokes the docked method of the base if it is'
        if self.ship.landed:
            for base in self.game_engine.bases:
                if base.rect.collidepoint(self.ship.rect.midbottom):
                    base.docked(self.ship,dt)
                    return 1
        return 0


    def _fire(self,dt):
        self.ship.prim_weapon.fire()
        self.changed = 1

    def _secFire(self,dt):
        self.ship.sec_weapon.fire()
        self.changed = 1

    def setStartingPos(self,pos):
        self.startingPos = pos


    def left(self,dt):
        self.ship.changeAng(self.ang_change,dt)
        self.changed = 1
    def right(self,dt):
        self.ship.changeAng(-self.ang_change,dt)
        self.changed = 1
    def up(self,dt):
        self.ship.setThrust(self.vel_change,dt)
        self.changed = 1
    def down(self,dt):
        self._secFire(dt)
        self.changed = 1
    def fire1(self,dt):
        self._fire(dt)
        self.changed = 1
    def fire2(self,dt):
        self.changed = 1
        self._secFire(dt)



    def sync(self,sync_info):
        ' Sync myself with sync dict '
        self.lives = sync_info['lives']
        self.score = sync_info['score']

    def getSync(self):
        ' Return information about player for syncing over net '
        sync_hash = {}
        sync_hash['score']=self.score
        sync_hash['lives']=self.lives
        return sync_hash
